package com.yls.autoapply.server;

import com.yls.autoapply.exception.SshRunException;
import com.yls.autoapply.exception.SshTimeoutException;
import com.yls.autoapply.model.SshResponse;
import com.yls.autoapply.util.CmdUtil;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;

@Slf4j
public class NodeServer extends ServerAbs {

    public SshResponse dockerTag(String source, String dest) throws IOException, SshTimeoutException, SshRunException {
        String cmd = String.format(CmdUtil.tag, source, dest);
        return super.runCmd(cmd);
    }

    public SshResponse dockerPush(String dest) throws IOException, SshTimeoutException, SshRunException {
        String cmd = String.format(CmdUtil.push, dest);
        return super.runCmd(cmd);
    }

    public SshResponse dockerImage(String service) throws IOException, SshTimeoutException, SshRunException {
        String cmd = String.format(CmdUtil.image, service);
        return super.runCmd(cmd);
    }

    @Override
    public void close() {
        try {
            if (getFs() != null) {
                getFs().close();
            }

            if (getSftpClient() != null) {
                getSftpClient().close();
            }

            if (getSession() != null) {
                getSession().close();
            }
        } catch (Exception e) {
            log.error("" + e);
        }
        getClient().stop();
    }

    public SshResponse run(String cmd) throws IOException, SshTimeoutException, SshRunException {
        return super.runCmd(cmd);
    }
}
